package com.tybbank.Activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.liji.imagezoom.util.ImageZoom;
import com.tybbank.Base.BaseActivity;
import com.tybbank.Bean.FenXiangBean;
import com.tybbank.Login.LoginActivity;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.tybbank.Utils.logUtilssss;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/9/27 0027.
 */

public class SharedSDKActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_erweiman_umber)
    TextView tvErweimanUmber;
    @BindView(R.id.iv_erweiman_image)
    ImageView ivErweimanImage;
    @BindView(R.id.tv_money_address)
    TextView tvMoneyAddress;
    @BindView(R.id.bt_copy)
    Button btCopy;
    private Intent intent;
    private String qrcord;
    private String money_path;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sharedsdk);
        ButterKnife.bind(this);
        toolbar(toolbar, getString(R.string.fenxiang), R.mipmap.icon_close);
        init();
    }

    private void init() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        initdata(time, sign);

    }

    private void initdata(String time, String sign) {
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.user_img)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        showTips(getString(R.string.error));
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG用户分享页面" + response.toString());
                        dialog.dismiss();
                        string(response);
                    }

                    private void string(String json) {
                        FenXiangBean enterBean = new Gson().fromJson(json, new TypeToken<FenXiangBean>() {
                        }.getType());
                        int status = enterBean.getCode();
                        if (status == 1011) {
                            FenXiangBean<FenXiangBean.DataBean> ent = new Gson().fromJson(json, new TypeToken<FenXiangBean<FenXiangBean.DataBean>>() {
                            }.getType());
                            money_path = ent.getData().getShare_path();
                            tvErweimanUmber.setText(ent.getData().getInvitation());
                            qrcord = ent.getData().getShare();
                            Glide.with(getBaseContext()).load(ent.getData().getShare_path()).into(ivErweimanImage);
                            tvMoneyAddress.setText(ent.getData().getShare());

                        } else if (status == 1004) {
                            intent = new Intent(getBaseContext(), LoginActivity.class);
                            startActivity(intent);
                            return;
                        } else {
                            showTips(enterBean.getMsg());
                        }
                    }
                });
    }

    @SuppressLint("WrongConstant")
    @OnClick({R.id.bt_copy, R.id.iv_erweiman_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_copy:
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", qrcord);
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                if (mClipData.toString().length() == 0) {
                    Toast.makeText(getBaseContext(), getString(R.string.copyno), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getBaseContext(), getString(R.string.copyok), Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.iv_erweiman_image:
                if (!TextUtils.isEmpty(money_path)) {
                    ImageZoom.show(this, money_path);
                }
                break;
        }
    }

//    @Override
//    public void RightNext() {
//        super.RightNext();
//        intent = new Intent(this, SDKListActivity.class);
//        startActivity(intent);
//    }

    @Override
    public void LeftClose() {
        super.LeftClose();
        SharedSDKActivity.this.finish();
    }
}
